﻿#define _CRT_SECURE_NO_WARNINGS 1
//⼆进制求和（easy）:https://leetcode.cn/problems/add-binary/
class Solution
{
public:
	string addBinary(string a, string b)
	{
		string ret;
		int cur1 = a.size() - 1, cur2 = b.size() - 1, t = 0;
		while (cur1 >= 0 || cur2 >= 0 || t)
		{
			if (cur1 >= 0) t += a[cur1--] - '0';
			if (cur2 >= 0) t += b[cur2--] - '0';
			ret += t % 2 + '0';
			t /= 2;
		}
		reverse(ret.begin(), ret.end());
		return ret;
	}
};
//字符串相乘（medium）:https://leetcode.cn/problems/multiply-strings/description/
class Solution {
public:
    string Mulstring(char ch, string s)
    {
        int end = s.size() - 1;

        string s1;
        int val = 0;
        while (end >= 0)
        {
            char x = s[end];
            int ret = (x - '0') * (ch - '0') + val;

            val = ret / 10;
            ret %= 10;

            s1 += ret + '0';

            end--;
        }

        if (val > 0)
        {
            s1 += val + '0';
        }

        reverse(s1.begin(), s1.end());
        return s1;
    }

    string Addstring(string s1, string s2)
    {
        int end1 = s1.size() - 1;
        int end2 = s2.size() - 1;

        int val = 0;
        string s;
        while (end1 >= 0 || end2 >= 0)
        {
            int x1 = end1 < 0 ? 0 : s1[end1] - '0';
            int x2 = end2 < 0 ? 0 : s2[end2] - '0';

            int sum = x1 + x2 + val;
            val = sum / 10;
            sum %= 10;

            s += sum + '0';

            end1--;   end2--;
        }

        if (val > 0)
        {
            s += val + '0';
        }

        reverse(s.begin(), s.end());
        return s;
    }
    string multiply(string num1, string num2) {

        reverse(num2.begin(), num2.end());
        int i = 0;
        int n = num2.size();

        string tmp, result;
        while (i < n)
        {
            tmp = Mulstring(num2[i], num1);

            if (i > 0)
            {
                string s;
                s.resize(i, '0');
                tmp += s;
            }

            result = Addstring(tmp, result);
            tmp.clear();

            i++;
        }
        if (result[0] == '0')
        {
            result = '0';
        }
        return result;

    }
};